home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / compstol / cmpsttl1.lha / CompositeTool / v1.1.dist / HDF / dfp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-08  |  10.1 KB  |  338 lines

  1. /*****************************************************************************
  2. *              NCSA HDF version 3.00
  3. *                December, 1989
  4. *
  5. * NCSA HDF Version 3.00 source code and documentation are in the public
  6. * domain.  Specifically, we give to the public domain all rights for future
  7. * licensing of the source code, all resale rights, and all publishing rights.
  8. * We ask, but do not require, that the following message be included in all
  9. * derived works:
  10. * Portions developed at the National Center for Supercomputing Applications at
  11. * the University of Illinois at Urbana-Champaign.
  12. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  13. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  14. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  15. *****************************************************************************/
  16.  
  17. /*-----------------------------------------------------------------------------
  18.  * File:    dfp.c
  19.  * Purpose: read and write palettes
  20.  * Invokes: df.c
  21.  * Contents: 
  22.  *  DFPgetpal: retrieve next palette
  23.  *  DFPputpal: write palette to file
  24.  *  DFPaddpal: add palette to file
  25.  *  DFPnpals: number of palettes in HDF file
  26.  *  DFPreadref: get palette with this reference number next
  27.  *  DFPwriteref: put palette with this reference number next
  28.  *  DFPrestart: forget info about last file accessed - restart from beginning
  29.  *  DFPlastref: return reference number of last element read or written
  30.  *  DFPIopen: open/reopen file
  31.  *---------------------------------------------------------------------------*/
  32.  
  33. #include "df.h"
  34.  
  35. static uint16 Readref=0;
  36. static uint16 Writeref=0;
  37. static uint16 Refset=0;        /* Ref of palette to get next */
  38. static uint16 Lastref = 0;    /* Last ref read/written */
  39.  
  40. static char Lastfile[DF_MAXFNLEN]; /* last file opened */
  41.  
  42. #ifndef VMS
  43. DF *DFPIopen();
  44. #else /*VMS*/
  45. DF *_DFPIopen();
  46. #endif
  47.  
  48.  
  49. /*-----------------------------------------------------------------------------
  50.  * Name:    DFPgetpal
  51.  * Purpose: get next palette from file
  52.  * Inputs:  filename: name of HDF file
  53.  *          palette: 768 byte space to read palette into
  54.  * Returns: 0 on success, -1 on failure with DFerror set
  55.  *          palette in pal
  56.  * Users:   HDF HLL users, utilities, other routines
  57.  * Invokes: DFPIopen, DFIerr, DFclose, DFgetelement
  58.  *---------------------------------------------------------------------------*/
  59.  
  60. int DFPgetpal(filename, palette)
  61. char *filename;
  62. char *palette;
  63. {
  64.     DF *dfile;
  65.     int cdd, ret;
  66.     DFdle *dlep;
  67.  
  68.     DFerror = DFE_NOERROR;
  69.  
  70.     if (!palette) {
  71.         DFerror = DFE_BADPTR;
  72.         return(-1);
  73.     }
  74.     dfile = DFPIopen(filename, DFACC_READ);
  75.     if (dfile == NULL) return(-1);
  76.  
  77.     if (Refset)
  78.         ret = DFIfind(dfile, DFTAG_IP8, Refset, 1,(uint16) 0, (uint16) 0,
  79.               &dlep, &cdd);
  80.     else
  81.         ret = DFIfind(dfile,DFTAG_IP8,0, !Readref, DFTAG_IP8,Readref,
  82.               &dlep,&cdd);
  83.  
  84.     Refset = 0;
  85.     if (ret<0) return(DFIerr(dfile)); /* on error, close file and return -1 */
  86.  
  87.     Readref = dlep->dd[cdd].ref; /* ref of element to read */
  88.  
  89.         /* read palette */
  90.     if (DFgetelement(dfile, DFTAG_IP8, Readref, palette)<0)
  91.             return(DFIerr(dfile));
  92.  
  93.     Lastref = Readref;
  94.  
  95.     return(DFclose(dfile));
  96. }
  97.  
  98.  
  99. /*-----------------------------------------------------------------------------
  100.  * Name:    DFPputpal
  101.  * Purpose: Write palette to file
  102.  * Inputs:  filename: name of HDF file
  103.  *          palette: palette to be written to file
  104.  *          overwrite: if 1, overwrite last palette read or written
  105.  *                     if 0, write it as a fresh palette
  106.  *          filemode: if "a", append palette to file
  107.  *                    if "w", create new file
  108.  * Returns: 0 on success, -1 on failure with DFerror set
  109.  * Users:   HDF users, programmers, utilities
  110.  * Invokes: DFPIopen, DFclose, DFputelement, DFIerr
  111.  * Remarks: To overwrite, the filename must be the same as for the previous
  112.  *          call
  113.  *---------------------------------------------------------------------------*/
  114.  
  115. int DFPputpal(filename, palette, overwrite, filemode)
  116. char *filename;
  117. char *palette;
  118. int overwrite;
  119. char *filemode;
  120. {
  121.     DF *dfile;
  122.  
  123.     DFerror = DFE_NOERROR;
  124.  
  125.     if (!palette) {
  126.         DFerror = DFE_BADPTR;
  127.         return(-1);
  128.     }
  129.  
  130.     if (overwrite && strcmp(filename, Lastfile)) {
  131.         DFerror = DFE_BADCALL;
  132.         return(-1);
  133.     }
  134.  
  135.     dfile = DFPIopen(filename, (*filemode=='w') ? DFACC_CREATE : DFACC_WRITE);
  136.     if (dfile==NULL) return(-1);
  137.  
  138.         /* if we want to overwrite, Lastref is the ref to write.  If not, if
  139.             Writeref is set, we use that ref.  If not we get a fresh ref. The
  140.             ref to write is placed in Lastref */
  141.     if (!overwrite) Lastref = Writeref ? Writeref : DFnewref(dfile);
  142.     if (Lastref == 0) return(-1);
  143.  
  144.     Writeref = 0;           /* don't know ref to write after this */
  145.  
  146.         /* write out palette */
  147.     if (DFputelement(dfile, DFTAG_IP8, Lastref, palette, (int32) 768)<0)
  148.             return(DFIerr(dfile));
  149.  
  150.     return(DFclose(dfile));
  151. }
  152.  
  153.  
  154. /*-----------------------------------------------------------------------------
  155.  * Name:    DFPaddpal
  156.  * Purpose: Add palette to file
  157.  * Inputs:  filename: name of HDF file
  158.  *          palette: palette to be written to file
  159.  * Returns: 0 on success, -1 on failure with DFerror set
  160.  * Users:   HDF users, programmers, utilities
  161.  * Invokes: DFPputpal
  162.  *---------------------------------------------------------------------------*/
  163.  
  164. int DFPaddpal(filename, palette)
  165. char *filename;
  166. char *palette;
  167. {
  168.  
  169.     return(DFPputpal(filename, palette, 0, "a"));
  170. }
  171.  
  172.  
  173. /*-----------------------------------------------------------------------------
  174.  * Name:    DFPnpals
  175.  * Purpose: How many palettes are present in this file?
  176.  * Inputs:  filename: name of HDF file
  177.  * Returns: number of palettes on success, -1 on failure with DFerror set
  178.  * Users:   HDF programmers, other routines and utilities
  179.  * Invokes: DFPIopen, DFclose, DFnumber
  180.  *---------------------------------------------------------------------------*/
  181.  
  182. int DFPnpals(filename)
  183. char *filename;
  184. {
  185.     DF *dfile;
  186.     int npals=0;
  187.  
  188.     DFerror = DFE_NOERROR;
  189.  
  190.     /* should use reopen if same file as last time - more efficient */
  191.     dfile = DFPIopen(filename, DFACC_READ);
  192.     if (dfile==NULL) return(-1);
  193.  
  194.     npals = DFnumber(dfile, DFTAG_IP8);    /* count number of IPs */
  195.     if (npals<0) return(DFIerr(dfile));
  196.  
  197.     if (DFclose(dfile)<0) return(-1);
  198.     return(npals);
  199. }
  200.  
  201.  
  202. /*-----------------------------------------------------------------------------
  203.  * Name:    DFPreadref
  204.  * Purpose: Set ref of palette to get next
  205.  * Inputs:  filename: file to which this applies
  206.  *          ref: reference number of next get
  207.  * Returns: 0 on success, -1 on failure
  208.  * Users:   HDF programmers, other routines and utilities
  209.  * Invokes: DFPIopen, DFIfind, DFclose
  210.  * Remarks: checks if palette with this ref exists
  211.  *---------------------------------------------------------------------------*/
  212.  
  213. int DFPreadref(filename, ref)
  214. char *filename;
  215. uint16 ref;
  216. {
  217.     DF *dfile;
  218.     int cdd;
  219.     DFdle *dlep;
  220.  
  221.     DFerror = DFE_NOERROR;
  222.  
  223.     dfile = DFPIopen(filename, DFACC_READ);
  224.     if (dfile==NULL) return(-1);
  225.  
  226.     if (DFIfind(dfile, DFTAG_IP8, ref, 1, 0, 0, &dlep, &cdd)<0)
  227.         return(DFIerr(dfile));
  228.  
  229.     Refset = ref;
  230.     return(DFclose(dfile));
  231. }
  232.  
  233.  
  234. /*-----------------------------------------------------------------------------
  235.  * Name:    DFPwriteref
  236.  * Purpose: Set ref of palette to put next
  237.  * Inputs:  filename: file to which this applies
  238.  *          ref: reference number of next put
  239.  * Returns: 0 on success, -1 on failure
  240.  * Users:   HDF programmers, other routines and utilities
  241.  * Invokes: none
  242.  *---------------------------------------------------------------------------*/
  243.  
  244. /* shut lint up */
  245. /* ARGSUSED */
  246. int DFPwriteref(filename, ref)
  247. char *filename;
  248. uint16 ref;
  249. {
  250.     DFerror = DFE_NOERROR;
  251.  
  252.     Writeref = ref;
  253.     return(0);
  254. }
  255.  
  256.  
  257.  
  258. /*-----------------------------------------------------------------------------
  259.  * Name:    DFPrestart
  260.  * Purpose: Do not remember info about file - get again from first palette
  261.  * Inputs:  none
  262.  * Returns: 0 on success
  263.  * Users:   HDF programmers
  264.  * Remarks: Just reset Lastfile to NULL
  265.  *---------------------------------------------------------------------------*/
  266.  
  267. int DFPrestart()
  268. {
  269.  
  270.     Lastfile[0] = '\0';
  271.     return(0);
  272. }
  273.  
  274.  
  275. /*-----------------------------------------------------------------------------
  276.  * Name:    DFPlastref
  277.  * Purpose: Return last ref written or read
  278.  * Inputs:  none
  279.  * Globals: Lastref
  280.  * Returns: ref on success, -1 on error with DFerror set
  281.  * Users:   HDF users, utilities, other routines
  282.  * Invokes: none
  283.  * Method:  return Lastref
  284.  * Remarks: none
  285.  *---------------------------------------------------------------------------*/
  286.  
  287. int DFPlastref()
  288. {
  289.  
  290.     return(Lastref);
  291. }
  292.  
  293.  
  294.  
  295. /******************************************************************************/
  296. /*----------------------- Internal routines ---------------------------------*/
  297. /******************************************************************************/
  298.  
  299.  
  300. /*-----------------------------------------------------------------------------
  301.  * Name:    DFPIopen
  302.  * Purpose: open or reopen a file
  303.  * Inputs:  filename: name of file to open
  304.  *          access : access mode
  305.  * Returns: file pointer on success, NULL on failure with DFerror set
  306.  * Users:   HDF systems programmers, other DFP routines
  307.  * Invokes: DFopen
  308.  * Remarks: This is a hook for someday providing more efficient ways to
  309.  *          reopen a file, to avoid re-reading all the headers
  310.  *---------------------------------------------------------------------------*/
  311.  
  312. DF *DFPIopen(filename, access)
  313. char *filename;
  314. int access;
  315. {
  316.  
  317.     DF *dfile;
  318.  
  319.         /* use reopen if same file as last time - more efficient */
  320.     if (strncmp(Lastfile,filename,DF_MAXFNLEN) || (access==DFACC_CREATE)) {
  321.                                     /* treat create as different file */
  322.         if (!(dfile = DFopen(filename, access, -1))) return(NULL);
  323.         Refset = 0;         /* no ref to get set for this file */
  324.         Readref = 0;
  325.     }
  326.     else
  327.         if (!(dfile = DFopen(filename, access, -1))) return(NULL);
  328.  
  329.     DFIstrncpy(Lastfile, filename, DF_MAXFNLEN);
  330.         /* remember filename, so reopen may be used next time if same file */
  331.     return(dfile);
  332. }
  333.